from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
db = SQLAlchemy()

# 地区模型
class Area(db.Model):
    __tablename__ = "tb_area"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(255),nullable=False)
    parent_id = db.Column(db.Integer,db.ForeignKey('tb_area.id'),nullable=True)
    parent = db.relationship('Court', remote_side=[id], backref='childs')

# 法院模型
class Court(db.Model):
    __tablename__ = "tb_court"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    number = db.Column(db.String(255),unique=True,nullable=False)
    name = db.Column(db.String(255),nullable=False)
    score = db.Column(db.Integer,nullable=False)
    area_id = db.Column(db.Integer,db.ForeignKey('tb_area.id'),nullable=True,unique=True)
    parent_id = db.Column(db.Integer,db.ForeignKey('tb_court.id'),nullable=True)
    parent = db.relationship('Court',remote_side=[id],backref='childs')
    area = db.relationship('Area',backref='court')

# 管理员模型
class Admin(db.Model):
    __tablename__ = "tb_admin"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    username = db.Column(db.String(255),unique=True,nullable=False)
    password = db.Column(db.String(255),nullable=False)
    court_id = db.Column(db.Integer,db.ForeignKey('tb_court.id'),nullable=False)
    court = db.relationship('Court',backref="admins")

# 部门模型
class Depart(db.Model):
    __tablename__ = "tb_depart"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(255),nullable=False)
    court_id = db.Column(db.Integer, db.ForeignKey('tb_court.id'), nullable=False)
    court = db.relationship('Court', backref="departs")

# 人员模型
class People(db.Model):
    __tablename__ = "tb_people"
    id = db.Column(db.Integer,primary_key=True,autoincrement=True)
    name = db.Column(db.String(255),nullable=False)
    # 性别:默认为男
    sex = db.Column(db.Boolean,default=True)
    # 工资
    salary = db.Column(db.Integer,nullable=False)
    depart_id = db.Column(db.Integer,db.ForeignKey('tb_depart'),nullable=False)
    depart = db.relationship('Job',backref='peoples')



def init_db(app):
    db.init_app(app)
    Migrate(app=app,db=db)
